<?php
/**
 * Created by PhpStorm.
 * User: liu85
 * Date: 2018/7/12
 * Time: 21:12
 */

namespace app\admin\controller;


use app\common\consts\AdminConst;
use app\common\consts\BaseConst;
use app\common\helper\SystemHelper;
use think\facade\Request;
use think\facade\View;

class Index extends Auth
{
    public function index()
    {
        $this->assign('admin', $this->admin);
        $this->assign('menu', $this->getMenu());
        return View::fetch();
    }

    public function home()
    {
        if($this->admin->type == AdminConst::TYPE_CHANNEL){
            $this->redirect('/admin/channel.get.data.list');exit;
        }
        $data['host_name'] = gethostname();
        $data['server_ip'] = $_SERVER['SERVER_ADDR'];
        $data['server_host'] = $_SERVER['HTTP_HOST'];
        $data['server_port'] = $_SERVER['SERVER_PORT'];
        $data['server_software'] = $_SERVER['SERVER_SOFTWARE'];
        $data['os'] = php_uname();
        $data['php_version'] = PHP_VERSION;
        $data['mysql_version'] = 'MySQL 5.6';
        $data['session_id'] = session_id();
        $data['memory'] = memory_usage();
        $data['time'] = date('Y-m-d H:i:s');
        $data['upload_max_filesize'] = get_cfg_var ("upload_max_filesize")?get_cfg_var ("upload_max_filesize"):"不允许上传附件";
        $data['session_count'] = count($_SESSION);
        $data['run_time'] = SystemHelper::getRunTime();
        $data['load_avg'] = SystemHelper::getSysLogAvg()[0];
        foreach($data as $k=>$v){
            $this->assign($k,$v);
        }
        $this->assign('info',$data);
        return View::fetch();
    }

    public function upload(){
        // 获取表单上传文件 例如上传了001.jpg
        $file = request()->file('file');
        // 移动到框架应用根目录/uploads/ 目录下
        $info = $file->validate(['ext'=>'jpg,png,gif'])->rule('md5')->move( './uploads');
        if(!$info){
            _failure('文件上传失败'.$file->getError());
        }
        $fileName = BaseConst::BASE_DOMAIN.'/uploads/'.$info->getSaveName();
        _success(['file'=>$fileName]);
    }
}